// created by noellynch
// May 19, 2011

package com.gruntdb.objects;

import java.nio.ByteBuffer;
import java.util.Iterator;

import com.gruntdb.core.stubs.IGruntDBIndex;
import com.gruntdb.core.stubs.IReadonlyGruntDBIndex;

public class GruntDBIndexIterator implements Iterator<IReadonlyGruntDBIndex> {

	private		GruntDBIndexStore		mcl_store;
	private		GruntDBFileSeek			mcl_seek;
	private		long					ml_indexSize;
	private		IGruntDBIndex			mcl_index;
	
	public	GruntDBIndexIterator(GruntDBIndexStore cl_store) {
		mcl_store = cl_store;
		mcl_seek = mcl_store.getASeek();
		ml_indexSize = mcl_store.getSizeofIndex();
		mcl_index = mcl_store.getAnIndexInstance();
	}
	
	@Override
	public boolean hasNext() {
		try {
			long		ll_size = mcl_store.getSizeOfIndexStore();
			if(mcl_seek.getCurrentSeek() < ll_size) {
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public IReadonlyGruntDBIndex next() {
		ByteBuffer		lcl_buffer = mcl_store.getBuffer();
		lcl_buffer.position((int)mcl_seek.getCurrentSeek());
		mcl_index.from(lcl_buffer);
		mcl_seek.offset(ml_indexSize);
		return mcl_index;
	}

	@Override
	public void remove() {
	}
	
}
